package com.zlb.app.modules.dict.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zlb.app.common.entity.R;
import com.zlb.app.modules.dict.entity.Dict;
import com.zlb.app.modules.dict.service.IDictService;
import com.zlb.app.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 字典表 控制层
 * @Author zWX1367238
 * @Date 2024-08-20 11:35:51
 */
@RestController
@RequestMapping("/dict")
@RequiredArgsConstructor
@Api(tags = "字典表")
public class DictController {

    private final IDictService dictService;

    @DeleteMapping("/{ids}")
    @ApiOperation("根据id集合删除字典表")
    public R<Boolean> deleteByIds(@PathVariable("ids") Long[] ids) {
        return R.ok(this.dictService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping("/getDictByType/{type}")
    @ApiModelProperty("根据dictType获取字典列表")
    public R<List<Dict>> getDictByType(@PathVariable("type") String type) {
        return R.ok(dictService.getDictByType(type));
    }

    @GetMapping("/getTreeDict")
    @ApiModelProperty("获取树形字典")
    public R<List<Dict>> getTreeDict() {
        return R.ok(dictService.getTreeDict());
    }

    @PostMapping
    @ApiOperation(value = "保存字典表", hidden = true)
    public R<Boolean> save(@RequestBody Dict dict) {
        return R.ok(this.dictService.saveOrUpdate(dict));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询字典表", hidden = true)
    public R<Dict> getById(@PathVariable("id") Long id) {
        return R.ok(this.dictService.getById(id));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询字典表数据", hidden = true)
    public R<IPage<Dict>> page() {
        return R.ok(this.dictService.page(PageUtil.buildPage(Dict.class)));
    }

}
